package com.archermind.boc.xml;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationContextFactory {
	private static String XML_PATH = "com/archermind/boc/xml/SuperContext.xml";
	private static Object lock = new Object();
	private static String filePath = XML_PATH;

	public static String getFilePath() {
		return filePath;
	}

	public static void setFilePath(String filePath) {
		ApplicationContextFactory.filePath = filePath;
		context = null;
	}

	static ClassPathXmlApplicationContext context = null;

	public static ClassPathXmlApplicationContext getContext() {
		synchronized (lock) {
			if (context == null)
				context = new ClassPathXmlApplicationContext(filePath);
		}
		return context;
	}

	public static ClassPathXmlApplicationContext getContextForTest(String path) {
		synchronized (lock) {
			if (filePath.equals(path)) {
				if (context != null)
					return context;
			} else {
				filePath = path;
				context = new ClassPathXmlApplicationContext(path);
			}
		}
		return context;
	}

	public static void reset() {
		filePath = XML_PATH;
		context = null;
	}
}
